package cn.edu.swufe.surverpark.struts2.action;

import javax.annotation.Resource;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.edu.swufe.surverpark.model.User;
import cn.edu.swufe.surverpark.struts2.service.UserService;
import cn.edu.swufe.surverpark.util.DataUtil;
import cn.edu.swufe.surverpark.util.ValidateUtil;

/**
 *@author 陈龙 E-mail:529914446@qq.com
 *@version创建时间:2015年4月29日下午7:57:23
 *类说明
 */
@Controller
@Scope("prototype")
public class RegAction extends BaseAction<User> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private UserService userService;
	
	@SkipValidation
	public String toRegPage(){
		return "regPage";
	}
	
	public String doReg(){
		model.setPassword(DataUtil.md5(model.getPassword()));
		userService.saveEntity(model);
		return SUCCESS;
	}
	private String confirmPassword;
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}
	@Override
	public void validate() {
		// TODO Auto-generated method stub
		//1.非空
		if(!ValidateUtil.isValid(model.getEmail())){
			addFieldError("email","email不能为空!");
		}
		if(!ValidateUtil.isValid(model.getPassword())){
			addFieldError("password","password不能为空");
		}
		if(!ValidateUtil.isValid(model.getNickName())){
			addFieldError("email", "nickName不能为空!");
		}
		if(hasErrors()){
			return ;
		}
		if(!model.getPassword().equals(confirmPassword)){
			addFieldError("password","密码不一致");
			return ;
		}
		if(userService.isRegisted(model.getEmail())){
			addFieldError("email","此Email已经被注册");
		}
	}

}

